clear all
set more off
cd "/home/andrei/Desktop/Dropbox/rolling elections"

*** 2009 and 2014 elections
use lworking,clear
keep if inlist(year,2009,2014) 
keep year state dcode mastercode thedate ststart res incomepc enrel enlang literate graduate vs_viab vs_nviab hlvote enlp winshare sfratio mark enpv partymark
save t_temp,replace
foreach var of varlist dcode vs_viab vs_nviab hlvote enlp winshare sfratio res thedate ststart mark enpv partymark incomepc enrel enlang literate graduate {
rename `var' `var'2
}
save t_temp2,replace

import delimited "adjacent districts post 2008.csv",clear
replace state=lower(state)
replace state="odisha" if state=="orissa"
replace state="jammu and kashmir" if regexm(state,"jammu")
replace state="nct of delhi" if state=="delhi" 
keep state pc pc2
rename (pc pc2)(dcode dcode2)
expand 2,gen(x)
gen dcode3=dcode2 if x==1
replace dcode2=dcode if x==1
replace dcode=dcode3 if x==1
drop dcode3 x
expand 2,gen(year)
recode year (0=2009)(1=2014)
merge m:1 year state dcode using t_temp,nogenerate keep(3)
merge m:1 year state dcode2 using t_temp2,nogenerate keep(3)
keep if res==res2
keep if thedate==ststart
keep if thedate2>thedate
drop if thedate2-thedate>10
save paired_districts,replace

*** 2004 election
use lworking,clear
keep if year==2004
keep year scode state dcode mastercode thedate ststart res incomepc enrel enlang literate graduate vs_viab vs_nviab hlvote enlp winshare sfratio mark enpv partymark
gen ids=scode*100+dcode 
save t_temp,replace 
foreach var of varlist state mastercode ids ststart thedate res vs_viab vs_nviab hlvote enlp winshare sfratio mark enpv partymark incomepc enrel enlang literate graduate {
rename `var' `var'2
}
save t_temp2,replace

import delimited "adjacent districts pre 2008.csv",clear
expand 2,gen(x)
gen ids3=ids2 if x==1
replace ids2=ids if x==1
replace ids=ids3 if x==1
drop ids3 x
merge m:1 ids using t_temp,nogenerate keep(3)
merge m:1 ids2 using t_temp2,nogenerate keep(3)
keep if thedate==ststart
keep if thedate2>thedate
drop if thedate2-thedate>10
keep if state==state2
keep if res==res2
*** combine 
append using paired_districts
save paired_districts,replace

erase t_temp.dta
erase t_temp2.dta

** build a table with the differences in independent variables
use paired_districts,clear
expand 2,gen(x)
replace year=9999 if x==1
replace incomepc=incomepc/1000
replace incomepc2=incomepc2/1000
foreach var of varlist incomepc enrel enlang literate graduate partymark enpv mark {
gen dp_`var'=abs(`var'2-`var')
gen od_`var'=`var'2-`var'
}
collapse (mean) dp_* od_*,by(year)
merge 1:1 year using t_reference,nogenerate
reshape long d_ ds_ dp_ od_ sd_,i(year) j(variable) string
replace sd_=od_/sd_

order year variable d_ ds_ dp_ od_ sd_
gen sorter=1 if variable=="enlang"
replace sorter=2 if variable=="enrel"
replace sorter=3 if variable=="graduate"
replace sorter=4 if variable=="literate"
replace sorter=5 if variable=="incomepc"
replace sorter=6 if variable=="mark"
replace sorter=7 if variable=="partymark"
replace sorter=8 if variable=="enpv" 

gsort -year sorter
export delimited "Structural Differences.csv",replace

** build a table with the t-tests
use paired_districts,clear
expand 2,gen(x)
replace year=9999 if x==1
matrix out=(0,0,0,0,0,0,0)

foreach j in 2004 2009 2014 9999 {
local k=0
foreach var of varlist vs_viab vs_nviab hlvote enlp {
local k=`k'+1
ttest `var'2==`var' if year==`j'
matrix add=(`j',`k',r(mu_2),r(mu_1),r(se),r(p),r(N_1))
matrix out=out\add
}
}
matrix out=out[2...,1..4],(out[2...,4]-out[2...,3]),out[2...,5...]
clear
svmat out
rename out1 year
gen variable="vs_viab" if out2==1
replace variable="vs_nviab" if out2==2
replace variable="hlvote" if out2==3
replace variable="enlp" if out2==4
forval j=3/6 {
tostring(out`j'),gen(_`j') format(%9.2f) force
}
tostring(out8),gen(_8) force
replace _5=_5+"*" if out7<0.1
replace _5=_5+"*" if out7<0.05
replace _5=_5+"*" if out7<0.01
drop out*
reshape long _,i(year variable) j(rub)
gen rubric="1 Earlier phase" if rub==3
replace rubric="2 Later phase" if rub==4
replace rubric="3 Difference" if rub==5
replace rubric="4 SE of difference" if rub==6
replace rubric="5 N of dyads" if rub==8
reshape wide _,i(year rubric) j(variable) string
gsort -year rubric
order year rubric _vs_viab _vs_nviab _hlvote _enlp
drop rub
export delimited "Dyad tests.csv",replace

*** logit
use paired_districts,clear
gen running =_n
keep running incomepc* enrel* enlang* literate* graduate*
foreach var of varlist incomepc enrel enlang literate graduate {
rename `var' `var'1
}
reshape long incomepc enrel enlang literate graduate,i(running) j(later)
recode later (1=0)(2=1)
gen illiterate=1-literate
logit later incomepc enrel enlang illiterate graduate
tabstat incomepc enrel enlang illiterate graduate,statistic(mean var sd n) by (later)
